import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	// 这个代表全局可以访问数据对象，就像是咱们在组件中声明的 data 属性
	state: {
		loginState: !!uni.getStorageSync('loginState') ? true : false,
		userInfo: !!uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : '',
		//固定位置用于测试，上线注释此2行，打开下两行
		// mylocation:!!uni.getStorageSync('mylocation')?uni.getStorageSync('mylocation'):{'latitude':'31.266270','longitude':'120.653360'},
		// mylocationState: true,
		mylocationState:!!uni.getStorageSync('mylocationState') ? true : false,
		mylocation:!!uni.getStorageSync('mylocation')?uni.getStorageSync('mylocation'):'',
		wxcode: !!uni.getStorageSync('wxcode') ? uni.getStorageSync('wxcode') : '',
		wxopenid: !!uni.getStorageSync('wxopenid') ? uni.getStorageSync('wxopenid') : '',
	},
	// 这个实时监听 state 内的数据对象变化，类似 咱们组件中的 computed 属性，会依赖 state 数据变化而变化
	getters: {

	},
	// 用来同步设置 state 的值
	mutations: {
		userLogin(state, userInfo) {
			state.loginState = true
			state.userInfo = userInfo
			uni.setStorageSync('loginState', 'ok')
			uni.setStorageSync('userInfo', userInfo)
		},
		userLogout(state) {
			state.loginState = false
			state.userInfo = {}
			uni.clearStorageSync('userInfo')
			uni.clearStorageSync('loginState')
		},
		getlocation(state,mylocation){
			state.mylocationState = true
			state.mylocation = mylocation
			uni.setStorageSync('mylocationState', 'ok')
			uni.setStorageSync('mylocation',mylocation)
		},
		getWxCode(state,wxcode){
			state.wxcode = wxcode
			uni.setStorageSync('wxcode')
		},
		getWxopenid(state,wxopenid){
			state.wxopenid = wxopenid
			uni.setStorageSync('wxopenid')
		}
	},
	// 通过提交 mutations 内部的方法，异步更新 state 的状态，官方推荐都使用这种方法比较合适
	actions: {
		userLoginAction(context, userInfo) {
			context.commit('userLogin', userInfo)
		},
		userLogoutAction(context) {
			context.commit('userLogout')
		},
		getlocationAction(context, mylocation){
			context.commit('getlocation', mylocation)
		},
		userGetWxcode(context, wxcode){
			context.commit('getWxCode', wxcode)
		},
		userGetWxopenid(context, wxopenid){
			context.commit('getWxopenid', wxopenid)
		}
	}
})
export default store
